#include "./../../lvgl/lvgl.h"
#include "./../../lvgl/demos/lv_demos.h"
#include "./../../lv_drivers/display/fbdev.h"
#include "./../../lv_drivers/indev/evdev.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>

#include "./../../inc/homePage.h"
#include "./../../inc/album.h"
#include "./../../inc/page_manager.h"
#include "./../../inc/setting.h"
#include "./../../inc/login.h"

static lv_obj_t * test_ta       = NULL;
static const char * test        = NULL;
static int clicked              = 0;
static uint32_t last_click_time = 0;
static int update_flag          = NONE;
static lv_obj_t * userid        = NULL;
static lv_obj_t * username      = NULL;
static lv_obj_t * userphone     = NULL;
static lv_obj_t * error_info    = NULL;

void show_login_btn(lv_obj_t * parent);
static void cancel_login_event(lv_event_t * e);
static void close_popup_event(lv_event_t * e);
static void show_popup_event_name(lv_event_t * e);
static void show_popup_event_phone(lv_event_t * e);
static void close_popup_event_double_click(lv_event_t * e);
static void confirm_btn_event(lv_event_t * e);
static void set_opa(lv_obj_t * parent);

void show_user_card(lv_obj_t * parent)
{
    if(user_mode_card != NULL) {
        lv_obj_del(user_mode_card);
        user_mode_card = NULL;
    }

    user_mode_card = lv_obj_create(parent);
    lv_obj_set_style_pad_all(user_mode_card, 0, 0);
    lv_obj_set_style_border_width(user_mode_card, 0, LV_PART_MAIN);
    lv_obj_set_size(user_mode_card, 700, 390);
    lv_obj_clear_flag(user_mode_card, LV_OBJ_FLAG_SCROLLABLE); // 不可滚动

    lv_obj_set_style_bg_opa(user_mode_card, LV_OPA_0, LV_PART_MAIN);     // 设置背景完全透明
    lv_obj_set_style_border_opa(user_mode_card, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
    lv_obj_set_style_shadow_opa(user_mode_card, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明

    lv_obj_t * user_avatar = lv_obj_create(user_mode_card);
    lv_obj_set_size(user_avatar, 144, 144);
    lv_obj_set_pos(user_avatar, 10, 10);

    lv_obj_set_style_radius(user_avatar, 10, LV_PART_MAIN);

    lv_obj_t * avatar_img = lv_img_create(user_avatar);
    if(user_currNode == NULL) {
        lv_img_set_src(avatar_img, "S:res/ui/user.png");
    } else {
        lv_img_set_src(avatar_img, "S:res/ui/cat.jpg");
    }
    lv_obj_set_style_radius(avatar_img, 0, LV_PART_MAIN);
    lv_obj_set_size(avatar_img, 144, 144);
    lv_obj_center(avatar_img);

    lv_obj_clear_flag(user_avatar, LV_OBJ_FLAG_SCROLLABLE); // 不可滚动

    lv_obj_set_style_border_width(user_avatar, 1, LV_PART_MAIN);                   // 添加边框（增强视觉效果）
    lv_obj_set_style_border_color(user_avatar, lv_color_hex(BLACK), LV_PART_MAIN); // 黑色边框

    lv_obj_set_style_bg_opa(user_avatar, LV_OPA_0, LV_PART_MAIN);     // 设置背景完全透明
    lv_obj_set_style_border_opa(user_avatar, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
    lv_obj_set_style_shadow_opa(user_avatar, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明

    // 用户卡片
    lv_obj_t * user_card = lv_list_create(user_mode_card);
    lv_obj_set_size(user_card, 526, 144);
    lv_obj_set_pos(user_card, 164, 10);
    lv_obj_set_style_radius(user_card, 0, LV_PART_MAIN);
    lv_obj_set_style_border_width(user_card, 0, LV_PART_MAIN);
    lv_obj_set_style_border_color(user_card, lv_color_hex(BLACK), LV_PART_MAIN);
    lv_obj_set_style_pad_all(user_card, 0, LV_PART_MAIN);
    lv_obj_set_style_bg_color(user_card, lv_color_hex(0xFFFFFF), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(user_card, LV_OPA_COVER, LV_PART_MAIN);
    lv_obj_clear_flag(user_card, LV_OBJ_FLAG_SCROLLABLE); // 禁用滚动

    lv_obj_set_style_bg_opa(user_card, LV_OPA_0, LV_PART_MAIN);     // 设置背景完全透明
    lv_obj_set_style_border_opa(user_card, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
    lv_obj_set_style_shadow_opa(user_card, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明

    if(user_currNode) {
        userid = create_advanced_list_item(user_card, "账号", user_currNode->data->userID);
        lv_obj_clear_flag(userid, LV_OBJ_FLAG_CLICKABLE); // 禁用点击
        username = create_advanced_list_item(user_card, "昵称", user_currNode->data->username);
        lv_obj_add_event_cb(username, show_popup_event_name, LV_EVENT_CLICKED, setting_screen);
        userphone = create_advanced_list_item(user_card, "手机号码", user_currNode->data->phone);
        lv_obj_add_event_cb(userphone, show_popup_event_phone, LV_EVENT_CLICKED, setting_screen);

    } else {
        userid = create_advanced_list_item(user_card, "账号", "未登录");
        lv_obj_clear_flag(userid, LV_OBJ_FLAG_CLICKABLE);
        username = create_advanced_list_item(user_card, "昵称", "");
        lv_obj_add_event_cb(username, open_login, LV_EVENT_CLICKED, setting_screen);
        userphone = create_advanced_list_item(user_card, "手机号码", "");
        lv_obj_add_event_cb(userphone, open_login, LV_EVENT_CLICKED, setting_screen);
    }
    set_opa(userid);
    set_opa(username);
    set_opa(userphone);
    show_login_btn(user_mode_card);
}

lv_obj_t * create_advanced_list_item(lv_obj_t * parent, const char * left_text, const char * middle_text)
{
    const char * icon = LV_SYMBOL_RIGHT;
    lv_obj_t * item   = lv_btn_create(parent);
    lv_obj_set_size(item, 526, 48);
    lv_obj_set_flex_flow(item, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(item, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_style_pad_all(item, 0, LV_PART_MAIN);
    lv_obj_set_style_radius(item, 0, LV_PART_MAIN);
    lv_obj_clear_flag(item, LV_OBJ_FLAG_SCROLLABLE);

    // 设置按钮样式和点击效果 - 修改边框为白色
    lv_obj_add_flag(item, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_set_style_bg_opa(item, LV_OPA_100, LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(item, LV_OPA_70, LV_STATE_PRESSED);
    lv_obj_set_style_bg_color(item, lv_color_hex(0xFFFFFF), LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(item, lv_color_hex(0xF0F0F0), LV_STATE_PRESSED);
    lv_obj_set_style_border_width(item, 1, LV_PART_MAIN);
    lv_obj_set_style_border_color(item, lv_color_hex(0xC0C0C0), LV_PART_MAIN);

    // 关键：禁用内部容器的点击和滚动
    // 左侧文本容器
    lv_obj_t * left_cont = lv_obj_create(item);
    lv_obj_set_size(left_cont, lv_pct(50), lv_pct(100)); // 高度100%填满
    lv_obj_set_style_bg_opa(left_cont, LV_OPA_0, LV_PART_MAIN);
    lv_obj_set_style_border_width(left_cont, 0, LV_PART_MAIN);
    lv_obj_clear_flag(left_cont, LV_OBJ_FLAG_CLICKABLE); // 禁用点击
    lv_obj_clear_flag(left_cont, LV_OBJ_FLAG_SCROLLABLE);

    lv_obj_t * left_label = lv_label_create(left_cont);
    lv_label_set_text(left_label, left_text);
    LV_FONT_DECLARE(SourceHanSansCN_Bold_20);
    lv_obj_set_style_text_font(left_label, &SourceHanSansCN_Bold_20, 0);
    lv_obj_set_style_text_color(left_label, lv_color_white(), 0); // 文字改为白色
    lv_obj_align(left_label, LV_ALIGN_LEFT_MID, 10, 0);           // 左边距10px
    lv_obj_clear_flag(left_label, LV_OBJ_FLAG_CLICKABLE);         // 禁用标签点击

    // 中间文本容器
    lv_obj_t * middle_cont = lv_obj_create(item);
    lv_obj_set_size(middle_cont, lv_pct(35), lv_pct(100));
    lv_obj_set_style_bg_opa(middle_cont, LV_OPA_0, LV_PART_MAIN);
    lv_obj_set_style_border_width(middle_cont, 0, LV_PART_MAIN);
    lv_obj_clear_flag(middle_cont, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_clear_flag(middle_cont, LV_OBJ_FLAG_SCROLLABLE);

    lv_obj_t * middle_label = lv_label_create(middle_cont);
    lv_label_set_text(middle_label, middle_text);
    lv_obj_set_style_text_font(middle_label, &SourceHanSansCN_Bold_20, 0);
    lv_obj_set_style_text_color(middle_label, lv_color_white(), 0); // 文字改为白色
    lv_obj_align(middle_label, LV_ALIGN_RIGHT_MID, -10, 0);         // 右边距10px
    lv_obj_clear_flag(middle_label, LV_OBJ_FLAG_CLICKABLE);

    // 右侧图标容器
    lv_obj_t * right_cont = lv_obj_create(item);
    lv_obj_set_size(right_cont, lv_pct(15), lv_pct(100));
    lv_obj_set_style_bg_opa(right_cont, LV_OPA_0, LV_PART_MAIN);
    lv_obj_set_style_border_width(right_cont, 0, LV_PART_MAIN);
    lv_obj_clear_flag(right_cont, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_clear_flag(right_cont, LV_OBJ_FLAG_SCROLLABLE);

    lv_obj_t * right_icon = lv_label_create(right_cont);
    lv_label_set_text(right_icon, icon);
    lv_obj_set_style_text_font(right_icon, &lv_font_montserrat_16, 0);
    lv_obj_set_style_text_color(right_icon, lv_color_white(), 0); // 图标改为白色
    lv_obj_align(right_icon, LV_ALIGN_LEFT_MID, 10, 0);
    lv_obj_clear_flag(right_icon, LV_OBJ_FLAG_CLICKABLE);

    return item;
}
void show_login_btn(lv_obj_t * parent)
{
    if(user_currNode) {
        lv_obj_t * cancel_btn = lv_btn_create(parent);
        lv_obj_set_size(cancel_btn, 100, 40);
        lv_obj_set_style_radius(cancel_btn, 10, LV_PART_MAIN);
        lv_obj_set_style_bg_color(cancel_btn, lv_color_hex(RED), LV_PART_MAIN);
        lv_obj_set_style_bg_opa(cancel_btn, LV_OPA_COVER, LV_PART_MAIN);
        lv_obj_align(cancel_btn, LV_ALIGN_BOTTOM_RIGHT, -20, -20);
        lv_obj_add_event_cb(cancel_btn, cancel_login_event, LV_EVENT_CLICKED, setting_screen);
        LV_FONT_DECLARE(SourceHanSansCN_Bold_20);

        lv_obj_t * cancel_label = lv_label_create(cancel_btn); // 注意：在按钮内部创建标签
        lv_label_set_text(cancel_label, "退出登录");           // 对标签设置文本
        lv_obj_center(cancel_label);
        lv_obj_set_style_text_font(cancel_label, &SourceHanSansCN_Bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
        lv_obj_set_style_border_opa(cancel_btn, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
        lv_obj_set_style_shadow_opa(cancel_btn, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明
    } else if(!user_currNode) {
        lv_obj_t * login_btn = lv_btn_create(parent);
        lv_obj_set_size(login_btn, 100, 40);
        lv_obj_set_style_radius(login_btn, 10, LV_PART_MAIN);
        lv_obj_set_style_bg_color(login_btn, lv_color_hex(0x0080FF), LV_PART_MAIN);
        lv_obj_set_style_bg_opa(login_btn, LV_OPA_COVER, LV_PART_MAIN);
        lv_obj_align(login_btn, LV_ALIGN_BOTTOM_RIGHT, -20, -20);
        lv_obj_add_event_cb(login_btn, open_login, LV_EVENT_CLICKED, setting_screen);

        LV_FONT_DECLARE(SourceHanSansCN_Bold_20);
        lv_obj_t * login_label = lv_label_create(login_btn); // 注意：在按钮内部创建标签
        lv_label_set_text(login_label, "登录");              // 对标签设置文本
        lv_obj_center(login_label);
        lv_obj_set_style_text_font(login_label, &SourceHanSansCN_Bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
        lv_obj_set_style_border_opa(login_btn, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
        lv_obj_set_style_shadow_opa(login_btn, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明
    }
}

static void show_popup_event_name(lv_event_t * e)
{
    lv_obj_t * parent = lv_event_get_target(e);
    show_updata_popup("修改昵称", user_currNode->data->username);
    update_flag = UPDATA_USERNAME;
}

static void show_popup_event_phone(lv_event_t * e)
{
    lv_obj_t * parent = lv_event_get_target(e);
    show_updata_popup("修改手机号", user_currNode->data->phone);
    update_flag = UPDATA_PHONE;
}

void show_updata_popup(const char * titleText, const char * contentText)
{
    // 创建半透明背景层
    lv_obj_t * overlay = lv_obj_create(setting_screen);
    lv_obj_clear_flag(overlay, LV_OBJ_FLAG_SCROLLABLE); // 不可滚动
    lv_obj_set_size(overlay, LV_HOR_RES, LV_VER_RES);
    lv_obj_set_style_bg_color(overlay, lv_color_black(), 0);
    lv_obj_set_style_bg_opa(overlay, LV_OPA_50, 0);
    lv_obj_set_style_border_width(overlay, 0, 0);
    lv_obj_center(overlay);

    // 创建弹窗
    lv_obj_t * popup = lv_obj_create(overlay);
    lv_obj_set_size(popup, 400, 300);
    lv_obj_set_style_radius(popup, 12, 0);
    lv_obj_set_style_bg_color(popup, lv_color_white(), 0);
    lv_obj_set_style_bg_opa(popup, LV_OPA_100, 0);
    lv_obj_set_style_border_width(popup, 2, 0);
    lv_obj_set_style_border_color(popup, lv_color_hex(0xE0E0E0), 0);
    lv_obj_set_style_shadow_width(popup, 20, 0);
    lv_obj_set_style_shadow_opa(popup, LV_OPA_30, 0);
    lv_obj_center(popup);

    // 添加标题
    lv_obj_t * title = lv_label_create(popup);
    lv_label_set_text(title, titleText);
    LV_FONT_DECLARE(SourceHanSansCN_Bold_20);
    lv_obj_set_style_text_font(title, &SourceHanSansCN_Bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(title, lv_color_hex(0x333333), 0);
    lv_obj_align(title, LV_ALIGN_TOP_MID, 0, 10);

    // 用户名输入框容器
    lv_obj_t * username_cont = lv_obj_create(popup);
    lv_obj_set_size(username_cont, 300, 50);
    lv_obj_set_style_bg_opa(username_cont, LV_OPA_0, 0);
    lv_obj_set_style_border_width(username_cont, 0, 0);
    lv_obj_set_style_pad_all(username_cont, 0, 0);
    lv_obj_align(username_cont, LV_ALIGN_TOP_MID, 0, 60);
    lv_obj_clear_flag(username_cont, LV_OBJ_FLAG_SCROLLABLE); // 不可滚动

    // 创建图标
    lv_obj_t * username_icon = lv_img_create(username_cont);
    lv_img_set_src(username_icon, "S:res/ui/updata.png");
    lv_obj_set_size(username_icon, 48, 48);
    lv_obj_set_pos(username_icon, 2, 2);

    // 创建输入框
    test_ta = lv_textarea_create(username_cont);
    lv_obj_set_size(test_ta, 250, 48);                                      // 宽度调整
    lv_obj_align_to(test_ta, username_icon, LV_ALIGN_OUT_RIGHT_MID, 10, 0); // 在图标右边10px
    lv_obj_set_style_radius(test_ta, 6, 0);
    lv_obj_set_style_border_width(test_ta, 1, 0);
    lv_obj_set_style_border_color(test_ta, lv_color_hex(0xCCCCCC), 0);
    lv_textarea_set_text(test_ta, contentText);
    LV_FONT_DECLARE(SourceHanSansCN_Bold_20);
    lv_obj_set_style_text_font(test_ta, &SourceHanSansCN_Bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_clear_flag(test_ta, LV_OBJ_FLAG_SCROLLABLE); // 不可滚动

    lv_obj_set_pos(test_ta, 50, 0);

    // 错误信息
    error_info = lv_label_create(popup);
    lv_obj_set_style_bg_opa(error_info, LV_OPA_0, LV_PART_MAIN); // 设置背景完全透明
    lv_obj_set_style_border_width(error_info, 0, LV_PART_MAIN);  // 移除边框
    lv_obj_clear_flag(error_info, LV_OBJ_FLAG_SCROLLABLE);       // 不可滚动

    LV_FONT_DECLARE(SourceHanSansCN_Bold_20);
    lv_obj_set_style_text_font(error_info, &SourceHanSansCN_Bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_label_set_text(error_info, "");
    lv_obj_align(error_info, LV_ALIGN_BOTTOM_MID, 0, -90);
    lv_obj_clear_flag(error_info, LV_OBJ_FLAG_SCROLLABLE); // 禁止标签滚动

    // 返回按钮
    lv_obj_t * back_btn = lv_btn_create(popup);
    lv_obj_set_size(back_btn, 80, 45);

    lv_obj_align(back_btn, LV_ALIGN_BOTTOM_MID, -100, -10);
    lv_obj_set_style_radius(back_btn, 8, 0);
    lv_obj_set_style_bg_color(back_btn, lv_color_hex(0x9E9E9E), 0);

    // 返回按钮文本
    lv_obj_t * back_label = lv_label_create(back_btn);
    lv_label_set_text(back_label, "返回");
    LV_FONT_DECLARE(SourceHanSansCN_Bold_20);
    lv_obj_set_style_text_font(back_label, &SourceHanSansCN_Bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(back_label, lv_color_white(), 0);
    lv_obj_center(back_label);

    // 确认按钮
    lv_obj_t * confirm_btn = lv_btn_create(popup);
    lv_obj_set_size(confirm_btn, 80, 45);

    lv_obj_align(confirm_btn, LV_ALIGN_BOTTOM_MID, 100, -10);
    lv_obj_set_style_radius(confirm_btn, 8, 0);
    lv_obj_set_style_bg_color(confirm_btn, lv_color_hex(0x2196F3), 0);

    // 确认按钮文本
    lv_obj_t * confirm_label = lv_label_create(confirm_btn);
    lv_label_set_text(confirm_label, "确认");
    LV_FONT_DECLARE(SourceHanSansCN_Bold_20);
    lv_obj_set_style_text_font(confirm_label, &SourceHanSansCN_Bold_20, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_color(confirm_label, lv_color_white(), 0);
    lv_obj_center(confirm_label);

    // 添加键盘
    kb = lv_keyboard_create(overlay);
    lv_obj_set_size(kb, LV_HOR_RES, LV_VER_RES / 2);
    lv_obj_align(kb, LV_ALIGN_BOTTOM_MID, 0, 20);
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN); // 初始隐藏

    // 为文本框添加事件回调
    lv_obj_add_event_cb(test_ta, ta_event_cb, LV_EVENT_ALL, kb);

    // 添加事件
    lv_obj_add_event_cb(confirm_btn, confirm_btn_event, LV_EVENT_CLICKED, overlay);
    lv_obj_add_event_cb(overlay, close_popup_event_double_click, LV_EVENT_CLICKED, overlay);
    lv_obj_add_event_cb(back_btn, close_popup_event, LV_EVENT_CLICKED, overlay);
}

static void cancel_login_event(lv_event_t * e)
{
    user_currNode = NULL;
    show_error_window("退出成功!", 1);
    show_user_card(setting_card);
    show_home_avatar(home_page);
}

// 关闭窗口事件
static void close_popup_event_double_click(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        uint32_t current_time = lv_tick_get();
        lv_obj_t * overlay    = (lv_obj_t *)lv_event_get_user_data(e);

        // 检查时间间隔
        if(current_time - last_click_time > 300) {
            clicked = 0; // 超过300ms，重置计数
        }

        clicked++;
        last_click_time = current_time;

        // 如果是第二次点击且在时间窗口内，认为是双击
        if(clicked == 2) {

            clicked = 0;

            if(overlay) {
                lv_obj_del(overlay);
            }
        }
    }
}

// 关闭窗口事件
static void close_popup_event(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        lv_obj_t * overlay = (lv_obj_t *)lv_event_get_user_data(e);

        if(overlay) {
            lv_obj_del(overlay);
        }
    }
}

// 确认按钮事件
static void confirm_btn_event(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        lv_obj_t * overlay = (lv_obj_t *)lv_event_get_user_data(e);
        test               = lv_textarea_get_text(test_ta);
        if(update_flag == UPDATA_USERNAME) {
            strcpy(user_currNode->data->username, test);
            // printf("username:%s\n", user_currNode->data->username);
        }
        bool is_valid_phone = false;
        if(update_flag == UPDATA_PHONE) {
            // 简单验证：11位数字
            if(test != NULL && strlen(test) == 11) {
                is_valid_phone = true;
                for(int i = 0; i < 11; i++) {
                    if(!isdigit(test[i])) {
                        is_valid_phone = false;
                        break;
                    }
                }
            }
            if(!is_valid_phone) {
                lv_label_set_text(error_info, "请输入有效的11位手机号");
                lv_obj_set_style_text_color(error_info, lv_color_hex(RED), 0);
                return;
            }
            strcpy(user_currNode->data->phone, test);
            lv_label_set_text(error_info, "");
            // lv_obj_set_style_text_color(error_info, lv_color_hex(BLACK), 0);
        }
        if(overlay) {
            lv_obj_del(overlay);
        }
        test = NULL;
        show_user_card(setting_card);
        update_flag = NONE;
        save_file_user(user_headNode);
    }
}

static void set_opa(lv_obj_t * parent)
{
    lv_obj_set_style_bg_opa(parent, LV_OPA_0, LV_PART_MAIN); // 设置背景完全透明
    // lv_obj_set_style_border_opa(parent, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
    lv_obj_set_style_shadow_opa(parent, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明
}